<?php

require_once WP_PLUGIN_DIR . '/diapucp-events/defines.php' ;
require_once DPEM_SMB_PATH. '/smb_basefield.php' ;

define( 'DATEPICK_CSS_FILE', '/css-datepicker/jquery-ui-1.8.15.custom.css');
define( 'DATEPICK_JSC_FILE', '/jquery-datepicker/jquery.ui.datepicker.min.js' );

define( 'DATEPICK_CSS_DIRPATH', DPEM_CSS_DIRPATH . DATEPICK_CSS_FILE );
define( 'DATEPICK_CSS_URLPATH', DPEM_CSS_URLPATH . DATEPICK_CSS_FILE );
define( 'DATEPICK_JSC_DIRPATH', DPEM_JSC_DIRPATH . DATEPICK_JSC_FILE );
define( 'DATEPICK_JSC_URLPATH', DPEM_JSC_URLPATH . DATEPICK_JSC_FILE );

class smartmeta_datepicker extends smb_basefieldbox {

    public function  __construct($fields) {
        $this->set_fields( $fields );
        $this->set_inputid( $fields['id'] );


        //these are custom functions on the construct.
        add_action( 'admin_print_styles', array( &$this, 'add_stylesheets' ) );
        add_action( 'admin_enqueue_scripts', array( &$this, 'add_js' ) );
        add_action( 'admin_head-post.php', array( &$this, 'call_js' ) );
        add_action( 'admin_head-post-new.php', array( &$this, 'call_js' ) );
    }

    //Callback for the builder functions.
    public static function build_me( $field_data, &$new_field_object ){
        $new_field_object = new smartmeta_datepicker($field_data);
    }

    public function show(){
        $value = $this->fetch_value();
        ?>
        <td>
            <label for="<?php echo $this->inputid; ?>"><?php echo $this->fields['name'] ?></label>
        </td>
        <td>
            <?php $this->add_required_label(); ?>
            <input
                id="<?php echo $this->inputid; ?>"
                name="<?php echo $this->inputid; ?>"
                value="<?php echo esc_html($value) ?>"
                type="text"
                size="8"
                readonly="yes"
            />
        </td>
        <?php
        $this->add_description();
    }

    public function save( $post_id ){
        $data = $_POST[ $this->inputid ];
        if ( empty( $data ) and isset($this->fields['default'] ) ){ $data = $this->fields['default']; }
        $data = wp_strip_all_tags($data);
        smb_basefieldbox::set($this->fields['id'], $data);
    }

    //Datepicker CSS calls
    public function add_stylesheets(){
        if( file_exists(DATEPICK_CSS_DIRPATH) ){
            wp_register_style( 'datepicker-css', DATEPICK_CSS_URLPATH );
            wp_enqueue_style( 'datepicker-css' );
        }
    }

    //Datepicker JS calls
    public function add_js(){
        if( file_exists(DATEPICK_JSC_DIRPATH) ){
            wp_register_script( 'datepicker-js', DATEPICK_JSC_URLPATH );
            wp_enqueue_script( 'datepicker-js' );
        }
    }

    public function call_js(){
        if ( !smb_basefieldbox::check_page( 'post' ) ) return;

        echo '<script type="text/javascript">';
            echo 'jQuery(document).ready(function(){';
            echo 'jQuery("#'. $this->inputid .'").datepicker();';
            echo '});';
        echo '</script>';
    }

} //End of the Class

        /*
        ?>
            <script type="text/javascript">
                jQuery(document).ready(function() {
                    jQuery( "#<?php echo $this->inputid; ?>" ).datepicker();
                });
            </script>
        <?php
        */

?>
